package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.hicore.HicoreAntifraudScore;
import com.ucredit.babel.common.model.hicore.HicoreAntifraudScoreData;
import com.ucredit.babel.common.model.hicore.HicoreAntifraudScoreRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;


/*
 * 黑瞳反欺诈分B
 *
 * @author:szw
 * */

public class HeiTongAntiFraudScoreBParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 黑瞳反欺诈分B
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/10 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        HicoreAntifraudScoreRecord obj = (HicoreAntifraudScoreRecord) model;
        HicoreAntifraudScoreData data = obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);
        StringBuilder txSb = getBaseInfo(obj);

        if (data != null) {
            // clientNo 客户流水号
            TxSb.append(data.getClientNo() == null ? "" : data.getClientNo()).append(CRT001);
            // serverNo 服务端流水号
            TxSb.append(data.getServerNo() == null ? "" : data.getServerNo()).append(CRT001);
            HicoreAntifraudScore data2 = data.getData();
            if (data2 != null) {
                // antifraudScore 反欺诈评分
                TxSb.append(data2.getAntifraudScore() == null ? "" : data2.getAntifraudScore()).append(CRT001);
                // ReasonCode 反欺诈原因码
                TxSb.append(data2.getReasonCode() == null ? "" : data2.getReasonCode()).append(CRT001);
            } else {
                TxSb.append(CRT001);
                TxSb.append(CRT001);
            }
            // 接口响应代码
            TxSb.append(data.getResponseCode() == null ? "" : data.getResponseCode()).append(CRT001);
            // 接口相应文本
            TxSb.append(data.getResponseText() == null ? "" : data.getResponseText()).append(CRT001);
            // 结果响应代码
            TxSb.append(data.getResult() == null ? "" : data.getResult()).append(CRT001);
            // 结果响应文本
            TxSb.append(data.getResultText() == null ? "" : data.getResultText()).append(CRT001);
            // 结果版本号
            TxSb.append(data.getVersion() == null ? "" : data.getVersion()).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return  data != null;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_heitong_antifraud_scoreb_inc";
    }

    @Override
    public String getType() {
        return RecordType.HICORE_ANTIFRAUD_SCORE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "黑瞳反欺诈分B";
    }
}
